package com.cslc.edas.demo.client.control;

import com.cslc.edas.demo.service.IAsyncService;
import com.cslc.edas.demo.service.IService;
import com.taobao.hsf.tbremoting.invoke.CallbackInvocationContext;
import com.taobao.hsf.tbremoting.invoke.HSFFuture;
import com.taobao.hsf.tbremoting.invoke.HSFResponseFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HsfController {

    @Autowired
    private IService service;
    @Autowired
    private IAsyncService asyncService;

    @GetMapping(value = "/hsf")
    public String hsf() {
        String result = service.process(null);
        System.out.println(result);
        return "hello," + result;
    }

    @GetMapping(value = "/hsf_async")
    public String hsf_async() {
        String result = asyncService.future("abc");
        String str2;
        try {
            HSFFuture hsfFuture = HSFResponseFuture.getFuture();
            str2 = (String) hsfFuture.getResponse(3000);
        } catch (Throwable t) {
            t.printStackTrace();
            str2 = "future-exception";
        }
        return "hello," + result + "," + str2;
    }

    @GetMapping(value = "/hsf_callback")
    public String hsf_callback() {
        CallbackInvocationContext.setContext("init");
        String result = asyncService.callback("dbc");
        CallbackInvocationContext.setContext(null);
        return "hello," + result;
    }
}
